package com.gamefx.utils
{
	import flash.utils.Proxy;

	/**
	 * 用于解决Object不能作为FLEX组件数据源的问题
	 * @author hzf
	 *
	 */
	public dynamic class ArrayEx extends Array
	{
		/**
		 *
		 */
		private var _proxy:ArrayProxy;

		public function get proxy():Object
		{
			return _proxy;
		}


		public function ArrayEx()
		{
			_proxy = new ArrayProxy(this);
		}
	}
}










/**
 * 数据代理
 * @author hzf
 *
 */
import com.gamefx.utils.ArrayEx;

import flash.utils.Proxy;
import flash.utils.flash_proxy;

dynamic class ArrayProxy extends Proxy
{
	private var _source:ArrayEx;
	private var _map:Object;

	public function ArrayProxy(source:ArrayEx)
	{
		_source = source;
		_map = new Object();
	}


	override flash_proxy function callProperty(methodName:*, ... args):*
	{
		return _map[methodName].apply(_map, args);
	}

	override flash_proxy function getProperty(name:*):*
	{
		return _map[name];
	}

	override flash_proxy function setProperty(name:*, value:*):void
	{
		if (value == null)
		{
			throw Error("ArrayEx cannot accept null data!");
		}

		if (_map.hasOwnProperty(name))
		{
			var index:int = _source.indexOf(_map[name]);
			_source[index] = value;
		}
		else
		{
			_source.push(value);
		}

		_map[name] = value;
	}

	override flash_proxy function deleteProperty(name:*):Boolean
	{
		if (_map.hasOwnProperty(name))
		{
			var index:int = _source.indexOf(_map[name]);
			if (index != -1)
			{
				_source.splice(index, 1);
				_map[name] = null;
				delete _map[name];
			}
			return true;
		}
		else
		{
			return false;
		}
	}

	override flash_proxy function hasProperty(name:*):Boolean
	{
		return _map.hasOwnProperty(name);
	}
}
